<template>
  <router-view></router-view>
  <van-tabbar v-model="active" route @change="onChange">
    <van-tabbar-item icon="home-o" to="/home">首页</van-tabbar-item>
    <van-tabbar-item icon="cart-o" to="/cart">购物车</van-tabbar-item>
    <van-tabbar-item icon="user-o" to="/my">我的</van-tabbar-item>
  </van-tabbar>
</template>

<script>
import { ref, getCurrentInstance } from 'vue'

import router from './router'

export default {
  setup() {
    const { proxy } = getCurrentInstance()
    const active = ref(0)
    // const lists = [1, 2, 3]

    const onChange = (index) => {
      console.log('index', index)
      // 购物车
      if (index == 1) {
        if (localStorage.getItem('isLogin')) {
          router.push('/cart')
        } else {
          proxy.$toast('请先登录')
          setTimeout(() => {
            router.push('/login')
          }, 500)
        }
      }
      // 我的页面
      if (index == 2) {
        if (localStorage.getItem('isLogin')) {
          router.push('/my')
        } else {
          proxy.$toast('请先登录')
          setTimeout(() => {
            router.push('/login')
          }, 500)
        }
      }
    }

    // 登录成功后的逻辑
    // const handleLoginSuccess = () => {
    //   localStorage.setItem('isLogin', true) // 设置登录状态
    //   localStorage.setItem('lists', JSON.stringify(lists))
    //   console.log(localStorage.getItem('lists'))
    //   router.push('/home') // 登录成功后跳转到首页
    // }

    return {
      active,
      onChange
      // handleLoginSuccess
    }
  }
}
</script>

<style scoped></style>
